<template>
  <el-dialog
    :title="state.title"
    @close="handleClose"
    width="1000"
    v-model="state.visible"
    :before-close="beforeClose"
    :loading="state.loading"
    :close-on-press-escape="false"
    :close-on-click-modal="false"
    append-to-body
    destroy-on-close
  >
    <div>
      <yt-crud
        v-bind="options"
        ref="crudRef"
        @onLoad="getData"
        :fun-props="{
        exportBtn: false,
        delBtn: true,
        addBtn: false,
      }"
        :table-props="{
        selection: true,
        delBtn: true,
        pageHide: false,
        viewBtn: false,
        editBtn: false,
        menuWidth: 100,

      }"
        :loading="state.loading"
        :total="state.total"
        v-model:page="state.page"
        v-model:query="state.query"
        @delFun="handleDelete"
        @openBeforeFun="openBeforeFun"
        @change="onChange"
      >
        <template #rightToolbar></template>
        <template #enableTime="scope:any">
          <span>{{ parseTime(scope.row.enableTime, '{y}-{m}-{d}') }}</span>
        </template>
        <template #expireTime="scope:any">
          <span>{{ parseTime(scope.row.expireTime, '{y}-{m}-{d}') }}</span>
        </template>
        <template #state="scope:any">
          <DictTag :options="CouponCodeStateEnum" :value="scope.row.state" />
        </template>
        <template #menuSlot></template>
      </yt-crud>
    </div>

    <template #footer>
      <el-button @click="handleClose" :loading="state.loading">{{t('crud.cancelText')}}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>

import YtCrud from '@/components/common/yt-crud.vue'
import { batchRemove, list, remove } from '@/api/business/couponCode'
import {ComponentInternalInstance} from 'vue'
import {parseTime} from '@/utils/ruoyi'
import {useI18n} from 'vue-i18n'
const { t } = useI18n()

const { proxy } = getCurrentInstance() as ComponentInternalInstance
const { CouponCodeStateEnum } = toRefs<any>(proxy?.useEnum('coupon_code_state_enum'))

const state = reactive({
  visible: false,
  loading: false,
  recharge: {},
  title: t('business.couponCode.titleText'),
  page: {
    pageSize: 30,
    pageNum: 1,
  },
  total: 0,
  query: {},
})

const coupon = ref<any>({})

//字典
const column = [
  {
    label: t('business.couponCode.tranId'),
    key: 'tranId',
    type: 'string',
    search: false,
    formHide: false,
    slot: false,
  },
  {
    label: t('business.couponCode.amount'),
    key: 'amount',
    type: 'string',
    search: false,
    formHide: false,
    slot: false,
  },
  {
    label: t('business.couponCode.enableTime'),
    key: 'enableTime',
    type: 'string',
    search: false,
    formHide: false,
    slot: true,
  },
  {
    label: t('business.couponCode.expireTime'),
    key: 'expireTime',
    type: 'string',
    search: false,
    formHide: false,
    slot: true,
  },
  {
    label: t('business.couponCode.state'),
    key: 'state',
    type: 'select',
    search: false,
    formHide: false,
    slot: true,
  },
  {
    label: t('business.couponCode.userName'),
    key: 'userName',
    type: 'string',
    search: false,
    formHide: false,
    slot: false,
  },
]

const openDialog = (row?: any) => {
  coupon.value = row
  state.visible = true
}

const handleClose = () => {
  state.visible = false
}


const beforeClose = (done: () => void) => {
  done()
}
defineExpose({
  openDialog,
})

const crudRef = ref()
const data = ref<any[]>([])
const getData = () => {
  state.loading = true
  list({
    ...state.page,
    ...state.query,
    ...{ couponId: coupon.value.id}
  })
    .then((res) => {
      data.value = res.data.rows
      state.total = res.data.total
    })
    .finally(() => {
      state.loading = false
    })
}

const openBeforeFun = ({ type, data }) => {
  if (type === 'add') { /* empty */ } else if (type === 'update') { /* empty */ }
}


const handleDelete = async (row: any) => {
  state.loading = true
  if (row instanceof Array) {
    batchRemove(row.map((m) => m.id)).then(()=>{
      ElMessage.success(t('operate.tooltip.success'))
      getData()
    }).finally(()=>{
      state.loading = false
    })
  } else {
    remove(row.id).then(()=>{
      ElMessage.success(t('operate.tooltip.success'))
      getData()
    }).finally(()=>{
      state.loading = false
    })
  }
}

const multipleSelection = ref<{ deviceId: string }[]>([])

const onChange = (data) => {
  multipleSelection.value = data
}

const options = reactive({
  ref: 'crudRef',
  data,
  column,
})
</script>
